package manager
{
	import loaderData.LoaderSys;

	public class LoadMgr
	{
		private static var instance:LoadMgr;
		
		/**
		 *加载对列 
		 */		
		private var arrList:Array = new Array();
		
		/**
		 *是否在加载 
		 */		
		private var isLoadering:Boolean = false;
		
		private var loadSuccess:Function;
		private var loadIoErr:Function;
		
		private var loadingObj:Object; //正在加载的列表对像
		
		public function LoadMgr()
		{
		}
		
		public static function get inst():LoadMgr
		{
			return instance ||= new LoadMgr();
		}
		
		/**
		 * 加载资源 
		 * @param arr　传入列表
		 * @param strName 名称
		 * @param startCallBack　加载开始回调， 	注意，开始加载回高函数．会压入名称作为参数
		 * @param callBack　完成回调  			注意．返回成功的时候会压入两个参数．名称，和参数
		 * @param ioErrCallBack　错误回调
		 * ＠param　parameter　附带参数
		 * 
		 */		
		public function loaderResource(arr:Array, strName:String ="", startCallBack:Function = null, callBack:Function = null, ioErrCallBack:Function = null, parameter:Object = null):void
		{
			
			var obj:Object = new Object();
			
			obj.arr = arr;
			obj.strName = strName;
			obj.start = startCallBack;
			obj.success = callBack;
			obj.ioerr = ioErrCallBack;
			obj.parameter = parameter;
			
			arrList.push(obj);
			
			if(isLoadering)
				return;
			
			load();
		}
		
		private function load():void
		{
		
			if(arrList.length <= 0)
			{
				endLoad();
				return;
			}
			isLoadering = true;
			loadingObj = null;
			
			var obj:Object = arrList.shift();
			loadingObj = obj;
			
			loadList(obj);
		}
		
		private function loadList(obj:Object):void
		{
			var arr:Array = obj.arr;
			
			if(arr.length <= 0)
			{
				success();//如果为空．直接完成
				return;
			}
			
			LoaderSys.inst.load(arr, start, success, null, ioerr);
			
		}
		
		private function start():void
		{
			loadingObj && loadingObj.start && loadingObj.start(loadingObj.strName);
		}		
		
		private function success():void
		{
			loadingObj && loadingObj.success && loadingObj.success(loadingObj.strName, loadingObj.parameter);	
			load();
		}
		
		private function ioerr():void
		{

			loadingObj && loadingObj.ioerr && loadingObj.ioerr();
			load();

		}

		private function endLoad():void
		{
			isLoadering = false;
		}
		
		/**
		 * 当前加载的进度　0-1之间 
		 * @return 
		 * 
		 */		
		public function get nProgressNum():Number
		{

			return LoaderSys.inst.nProgress;
		}
	}
}